package com.lee.treasure.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lee.starter.response.Result;
import com.lee.treasure.entity.Columns;
import com.lee.treasure.entity.vo.Tables;
import com.lee.treasure.service.IColumnsService;
import com.lee.treasure.service.ITablesService;
import com.lee.treasure.vo.GenerateParams;
import com.lee.treasure.vo.GenerateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Lee
 * @date 2020/4/20
 */
@RestController
@RequestMapping("/db")
public class DbController {

    @Autowired
    private ITablesService tablesService;

    @Autowired
    private IColumnsService columnsService;

    @Value("${db.default}")
    private String defaultDb;


    @GetMapping("default")
    public Result defaultDb() {
        return Result.success(defaultDb);
    }

    @GetMapping("tables/{db}")
    public List<Tables> tables(@PathVariable("db") String db) {
        return tablesService.lambdaQuery().eq(Tables::getTableSchema, db).list();
    }


    @GetMapping("columns/{db}/{table}")
    public List<Columns> columns(@PathVariable("db") String db, @PathVariable("table") String table) {

        return columnsService.lambdaQuery()
                .eq(Columns::getTableSchema, db)
                .eq(Columns::getTableName, table)
                .list();
    }

    @PostMapping("generate")
    public GenerateVO generate(@RequestBody @Valid GenerateParams params) {
        return columnsService.generate(params);
    }

    @GetMapping("hi")
    public Object posture() {
        return 8;
    }

    @GetMapping("page")
    public IPage<Columns> page() {
        Page<Columns> page = new Page<>(1, 5);
        return columnsService.lambdaQuery().page(page);
    }


}
